//
//  HZL_PlayViewController.m
//  B_Project
//
//  Created by dllo on 15/12/5.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_PlayViewController.h"
#import "STKAudioPlayer.h"
#import "UIImageView+WebCache.h"

@interface HZL_PlayViewController ()
@property (nonatomic, retain) STKAudioPlayer * player;
@property (weak, nonatomic) IBOutlet UIButton *playButton;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *leftTime;
@property (weak, nonatomic) IBOutlet UISlider *progressSlider;
@property (weak, nonatomic) IBOutlet UIButton *backButton;
@property (nonatomic, retain) NSTimer *timer;
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UIImageView *bgImg;
@property (weak, nonatomic) IBOutlet UIImageView *bg;
@property (weak, nonatomic) IBOutlet UILabel *mediaName;

@property (weak, nonatomic) IBOutlet UILabel *nick;

@end

@implementation HZL_PlayViewController
// vc单例
+ (instancetype)sharePlayerViewController
{
    static HZL_PlayViewController * player = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        player = [[HZL_PlayViewController alloc] init];
                
    });
    
    return player;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.navigationController.navigationBar.hidden = YES;
    self.bgImg.contentMode = UIViewContentModeScaleAspectFill;
    self.bgImg.clipsToBounds = YES;
    
    
    UILabel *ing = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 60, 40)];
    ing.textColor = [UIColor colorWithRed:28/255.0 green:175/255.0 blue:239/255.0 alpha:1];
    ing.font = [UIFont fontWithName:@"KohinoorDevanagari-Medium" size:34];
    ing.text = @"ING";
    [self.view addSubview:ing];
    
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(75, 25, 1.5, 30)];
    line.backgroundColor = [UIColor colorWithRed:28/255.0 green:175/255.0 blue:239/255.0 alpha:1];
    [self.view addSubview:line];
    
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 20, 40, 40)];
    textLabel.numberOfLines = 0;
    textLabel.font = [UIFont systemFontOfSize:14];
    textLabel.textColor = [UIColor colorWithRed:28/255.0 green:175/255.0 blue:239/255.0 alpha:1];
    textLabel.text = @"最硬排行";
    [self.view addSubview:textLabel];
    

    // 背景高斯模糊
    UIBlurEffect *b = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *bV = [[UIVisualEffectView alloc] initWithEffect:b];
    bV.frame = self.view.frame;
    [self.bg addSubview:bV];
    
   
    
    //进来就播放
    [self playMusic];
    
}
- (void)playMusic {
    [self.playButton setImage:[UIImage imageNamed:@"iconfont-bofang-2"] forState:UIControlStateNormal];
    // 创建播放器
    [self creatAudioPlayer];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(track) userInfo:nil repeats:YES];
}
- (void)creatAudioPlayer
{
    // 先停止
    if (self.url == nil) {
        [self stopButton];
    }
    
    // 创建播放器单例
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        self.player = [[STKAudioPlayer alloc] initWithOptions:(STKAudioPlayerOptions){ .flushQueueOnSeek = YES, .enableVolumeMixer = YES, .equalizerBandFrequencies = {50, 100, 200, 400, 800, 1600, 2600, 16000} }];;
    });
    
    if (self.url == nil) {
        self.url = [NSURL URLWithString:self.model.normal_url];
        self.mediaName.text = self.model.mediaName;
        self.nick.text = self.model.author[@"nick"];
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:self.model.big_cover] placeholderImage:nil];
        [self.bg sd_setImageWithURL:[NSURL URLWithString:self.model.big_cover] placeholderImage:nil];
    }
    [self.player play:[NSString stringWithFormat:@"%@", self.url]];
}
- (IBAction)playButton:(UIButton *)sender {
    
    if (self.player.state == STKAudioPlayerStatePaused) {
        [self.playButton setImage:[UIImage imageNamed:@"iconfont-bofang-2"] forState:UIControlStateNormal];
        [self.player resume];
        
    }else if (self.player.state == STKAudioPlayerStatePlaying) {
        [self.playButton setImage:[UIImage imageNamed:@"iconfont-bofangzanting02"] forState:UIControlStateNormal];
        [self.player pause];
        
    }else {
        [self playMusic];
    }
}
- (IBAction)nextButton:(UIButton *)sender {
    [self.timer invalidate];
    self.indexRow++;
    if (self.indexRow == self.allMusicArray.count -1) {
        [self setIndexRow:0];
    }
    self.mediaName.text = [self.allMusicArray[self.indexRow] mediaName];
    self.nick.text = [self.allMusicArray[self.indexRow] author][@"nick"];
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:[self.allMusicArray[self.indexRow] big_cover]]placeholderImage:nil];
    [self.bg sd_setImageWithURL:[NSURL URLWithString:[self.allMusicArray[self.indexRow] big_cover]]placeholderImage:nil];
    
    [self.playButton setImage:[UIImage imageNamed:@"iconfont-bofang-2"] forState:UIControlStateNormal];
    
    [self setUrl:[NSURL URLWithString:[self.allMusicArray[self.indexRow] normal_url]]];
    
}
- (IBAction)backButton:(UIButton *)sender {
    if (self.indexRow != 0) {
        self.indexRow--;
        // 第一首返回最后一首
    }else{
        [self setIndexRow:self.allMusicArray.count - 1];
    }
    self.mediaName.text = [self.allMusicArray[self.indexRow] mediaName];
    self.nick.text = [self.allMusicArray[self.indexRow] author][@"nick"];
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:[self.allMusicArray[self.indexRow] big_cover]]];
    [self.bg sd_setImageWithURL:[NSURL URLWithString:[self.allMusicArray[self.indexRow] big_cover]]];
    
    [self.playButton setImage:[UIImage imageNamed:@"iconfont-bofang-2"] forState:UIControlStateNormal];
    [self setUrl:[NSURL URLWithString:[self.allMusicArray[self.indexRow] normal_url]]];
}

- (void)stopButton
{
    [self.player stop];
    [self.player clearQueue];
    [self.timer invalidate];
    [self setTimer:nil];
    [self.progressSlider setValue:0.f];
    [self.playButton setTitle:@"播放" forState:UIControlStateNormal];
}
- (IBAction)progressSlider:(UISlider *)sender {
    if (self.player) {
        
        [self.player seekToTime:[sender value]];
        
    }
}

- (void)setUrl:(NSURL *)url
{
    if (_url != url) {
        _url = url;
        
    }
    [self.timer invalidate];
    if (self.imgView) {
        [self playMusic];
    }
    

}
-(void)track
{
    self.progressSlider.maximumValue = self.player.duration;
    self.progressSlider.value = self.player.progress;
    
    NSInteger proMin = (NSInteger)self.player.progress / 60;
    NSInteger proSec = (NSInteger)self.player.progress % 60;
    
    NSInteger durMin = (NSInteger)self.player.duration / 60;
    NSInteger durSec = (NSInteger)self.player.duration % 60;
    
    self.timeLabel.font = [UIFont systemFontOfSize:14];
    self.leftTime.font = [UIFont systemFontOfSize:14];

    
    [[self timeLabel] setText:[NSString stringWithFormat:@"%02ld:%02ld",durMin,durSec]];
    self.leftTime.text = [NSString stringWithFormat:@"%02ld:%02ld", proMin,proSec];
}
#pragma mark - 返回
- (IBAction)back:(UIButton *)sender {
    [self stopButton];
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
